package cn.tedu.bece.front.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.tedu.bece.front.mapper.BabyMapper;
import cn.tedu.bece.front.pojo.dto.BabyQuery;
import cn.tedu.bece.front.pojo.dto.BabySaveParam;
import cn.tedu.bece.front.pojo.entity.Baby;
import cn.tedu.bece.front.pojo.vo.BabyVO;
import cn.tedu.bece.front.service.BabyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class BabyServiceImpl implements BabyService {

    @Autowired
    BabyMapper babyMapper;

    @Override
    public void saveBaby(BabySaveParam babySaveParam) {
        log.debug("保存宝宝业务，参数：{}",babySaveParam);
        Baby baby = new Baby();
        BeanUtils.copyProperties(babySaveParam,baby);
        if(baby.getId() == null){
            Integer userId = StpUtil.getLoginIdAsInt();
            baby.setUserId(userId);
            baby.setCreateTime(new Date());
            babyMapper.insert(baby);
        }
    }

    @Override
    public List<BabyVO> selectBaby(BabyQuery babyQuery) {
        log.debug("查询宝宝业务：参数：{}",babyQuery);
        List<BabyVO> list = babyMapper.selectBaby(babyQuery);
        return list;
    }

    @Override
    public BabyVO selectBabyById(BabyQuery babyQuery) {
        log.debug("通过id查询宝宝业务：参数：{}",babyQuery);
        BabyVO babyVO = babyMapper.selectBabyById(babyQuery);
        return babyVO;
    }
}
